<?php

require_once(BASE_PATH."src/Object/Location/Province.php");
require_once("GenericReader.php");

class ProvinceReader extends GenericReader{
		
	public function __construct($object = NULL, $connection = NULL){
		parent::__construct($object, $connection);
	}
	
	public function execute(){
		$this->select = new RXSelect($this->connection);
		
		switch($this->operation){
			default: 
				return $this->executeGetOne();
		}
	}
		
	public function executeGetOne(){
		$this->select->addTable("provinces");
		$this->select->addField("*");
		$this->select->addWhereAndStatementEquals("id", $this->object->getId());
		return $this->select->execute();
	}
	
	public function executeTotal(){
		$this->select->addTable("provinces");
		$this->select->addField("COUNT(*)", "total");
		return $this->select->execute();
	}
	
	public function next(){
		$row = $this->select->next();
		if($row){
			return new Province(	$row["id"], 
												utf8_decode($row["name"]), 
												utf8_decode($row["normalized_name"]));	
		}else{
			return FALSE;
		}
	}
	
	public function getTotal(){
		$row = $this->select->next();
		return $row["total"];
	}
}